package com.example.myapplication

import com.example.myapplication.my_view_model.ViewModePlus
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.myapplication.databinding.ActivityPreferencesBinding

class PreferencesActivity : AppCompatActivity() {
//    private lateinit var pre: SharedPreferences
//    lateinit var edit: SharedPreferences.Editor
    lateinit var binding: ActivityPreferencesBinding
    lateinit var viewModel: ViewModePlus
    private val key: String = "MyKey"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView<ActivityPreferencesBinding>(this,R.layout.activity_preferences)
        viewModel = ViewModelProvider(this)[ViewModePlus::class.java]
        binding.data = viewModel
        binding.lifecycleOwner = this
        viewModel.getLifeData()
        viewModel.getLifeData().observe(this,object: Observer<Int> {
            override fun onChanged(value: Int) {
                Log.d("TAG", value.toString())
            }
        })
        //不使用LifeData实现
//        pre = getSharedPreferences("AppCompatActivity", MODE_PRIVATE)
//        var defaltV = pre.getInt(key,0)
//        edit = pre.edit()
//        binding.textView13.text = pre.getInt(key,0).toString()
//        binding.button14.setOnClickListener(object: View.OnClickListener{
//            override fun onClick(v: View?) {
//                defaltV+=1
//                binding.textView13.text = defaltV.toString()
//                edit.putInt(key,defaltV)
//                edit.commit()
//            }
//        })
    }
}